home *** CD-ROM | disk | FTP | other *** search
- unit ShowU1;
-
- interface
-
- uses
- Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- ExtCtrls, StdCtrls, VSortFns;
-
- const
- ElemCount = 300;
-
- type
- PSortArray = ^TSortArray;
- TSortArray = array [0..pred(ElemCount)] of TSortElement;
-
- type
- TForm1 = class(TForm)
- ButtonSort: TButton;
- PaintBox1: TPaintBox;
- ListBox1: TListBox;
- ButtonRandomize: TButton;
- ButtonReverse: TButton;
- Memo1: TMemo;
- ButtonAlmost: TButton;
- Label1: TLabel;
- procedure ButtonSortClick(Sender: TObject);
- procedure PaintBox1Paint(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- procedure ButtonRandomizeClick(Sender: TObject);
- procedure ButtonReverseClick(Sender: TObject);
- procedure ButtonAlmostClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- SA : PSortArray;
- procedure ClearLabel;
- procedure SetLabelToDone;
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.DFM}
-
- procedure DrawLine(Canvas : TCanvas;
- Line : integer;
- Len : integer);
- begin
- Canvas.MoveTo(3, Line + 1);
- Canvas.LineTo(3+Len, Line + 1);
- end;
-
- function LessAndShow(X, Y : TSortElement) : boolean; far;
- begin
- Result := X < Y;
- end;
-
- procedure SwapAndShow(var A : array of TSortElement;
- I, J : integer); far;
- var
- Temp : TSortElement;
- begin
- {paint out the current lines}
- Form1.PaintBox1.Canvas.Pen.Color := clBtnFace;
- DrawLine(Form1.PaintBox1.Canvas, I, A[I]);
- DrawLine(Form1.PaintBox1.Canvas, J, A[J]);
- Temp := A[I];
- A[I] := A[J];
- A[J] := Temp;
- Form1.PaintBox1.Canvas.Pen.Color := clRed;
- DrawLine(Form1.PaintBox1.Canvas, I, A[I]);
- DrawLine(Form1.PaintBox1.Canvas, J, A[J]);
- end;
-
- procedure SetAndShow(var A : array of TSortElement;
- X : TSortElement;
- I : integer); far;
- begin
- Form1.PaintBox1.Canvas.Pen.Color := clBtnFace;
- DrawLine(Form1.PaintBox1.Canvas, I, A[I]);
- A[I] := X;
- Form1.PaintBox1.Canvas.Pen.Color := clRed;
- DrawLine(Form1.PaintBox1.Canvas, I, A[I]);
- end;
-
- procedure TForm1.ButtonRandomizeClick(Sender: TObject);
- var
- i : integer;
- MaxLen : integer;
- begin
- ClearLabel;
- Randomize;
- MaxLen := PaintBox1.Width-6;
- for i := 0 to pred(ElemCount) do
- SA^[i] := Random(MaxLen);
- PaintBox1.Repaint;
- end;
-
- procedure TForm1.ButtonSortClick(Sender: TObject);
- begin
- ClearLabel;
- case ListBox1.ItemIndex of
- 0 : VisualBubbleSort(SA^, 0, pred(ElemCount), LessAndShow, SwapAndShow);
- 1 : VisualShakerSort(SA^, 0, pred(ElemCount), LessAndShow, SwapAndShow);
- 2 : VisualSelectionSort(SA^, 0, pred(ElemCount), LessAndShow, SwapAndShow);
- 3 : VisualInsertionSort(SA^, 0, pred(ElemCount), LessAndShow, SetAndShow);
- 4 : VisualBestInsertionSort(SA^, 0, pred(ElemCount), LessAndShow, SwapAndShow, SetAndShow);
- 5 : VisualShellsort(SA^, 0, pred(ElemCount), LessAndShow, SwapAndShow, SetAndShow);
- 6 : VisualQuicksort(SA^, 0, pred(ElemCount), LessAndShow, SwapAndShow);
- 7 : VisualBestQuicksort(SA^, 0, pred(ElemCount), LessAndShow, SwapAndShow, SetAndShow);
- end;
- SetLabelToDone;
- end;
-
- procedure TForm1.PaintBox1Paint(Sender: TObject);
- var
- i : integer;
- begin
- if SA <> nil then begin
- PaintBox1.Canvas.Pen.Color := clRed;
- for i := 0 to pred(ElemCount) do
- DrawLine(PaintBox1.Canvas, i, SA^[i]);
- end;
- end;
-
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- New(SA);
- ButtonRandomizeClick(Self);
- ListBox1.ItemIndex := 0;
- end;
-
- procedure TForm1.FormDestroy(Sender: TObject);
- begin
- Dispose(SA);
- SA := nil;
- end;
-
- procedure TForm1.ButtonReverseClick(Sender: TObject);
- var
- i : integer;
- begin
- ClearLabel;
- for i := 0 to pred(ElemCount) do
- SA^[i] := ElemCount - i;
- PaintBox1.Repaint;
- end;
-
- procedure TForm1.ButtonAlmostClick(Sender: TObject);
- var
- i : integer;
- OtherInx : integer;
- Temp : TSortElement;
- begin
- ClearLabel;
- Randomize;
- for i := 0 to pred(ElemCount) do
- SA^[i] := i;
- for i := 0 to pred(ElemCount) do
- if Random(100) < 5 then begin
- OtherInx := Random(ElemCount);
- Temp := SA^[i];
- SA^[i] := SA^[OtherInx];
- SA^[OtherInx] := Temp;
- end;
- PaintBox1.Repaint;
- end;
-
- procedure TForm1.ClearLabel;
- begin
- Label1.Caption := '';
- Label1.Update;
- end;
-
- procedure TForm1.SetLabelToDone;
- begin
- Label1.Caption := 'Done!';
- Label1.Update;
- end;
-
- end.
-